package com.jzy.message.activities.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.jzy.message.R;
import com.jzy.message.activities.ChengyuanActivity;
import com.jzy.message.activities.base.AppConfig;
import com.jzy.message.activities.bean.ChengYuanBean;
import com.mcxtzhang.swipemenulib.SwipeMenuLayout;
import com.squareup.picasso.MemoryPolicy;
import com.squareup.picasso.NetworkPolicy;
import com.squareup.picasso.Picasso;

import java.util.List;

import static com.jzy.message.R.drawable.touxiang;

/**
 * Created by Administrator on 2018/4/3 0003.
 */

public class ChengYuanAdapter extends XRecyclerView.Adapter<ChengYuanAdapter.ViewHoler> {
    private Context mContext;
    private List<ChengYuanBean.DataBean> beanList;
    private ChengyuanActivity chengyuanActivity;
    private int userid;
    private int Distinguishid;

    public ChengYuanAdapter(List<ChengYuanBean.DataBean> beanList, ChengyuanActivity chengyuanActivity, int userid) {
        this.beanList=beanList;
        mContext=chengyuanActivity;
        this.chengyuanActivity=chengyuanActivity;
        this.userid=userid;
    }


    @Override
    public ChengYuanAdapter.ViewHoler onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.chengyuan_item, parent, false);
        ChengYuanAdapter.ViewHoler holder = new ChengYuanAdapter.ViewHoler(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final ChengYuanAdapter.ViewHoler holder, final int position) {

        if(beanList.get(position).getDistinguish()==1){
            Distinguishid=beanList.get(position).getUserid();
        }
        String tv_nick=beanList.get(position).getRealName()==
                null?"":beanList.get(position).getRealName();
        holder.tv_nick.setText(tv_nick);

        Picasso.with(mContext).load(AppConfig.JASON_SERVICE_URL_TOUXIANG+"upload/"+beanList.get(position).getUserName()+".jpg")
                .memoryPolicy(MemoryPolicy.NO_CACHE)

                .networkPolicy(NetworkPolicy.NO_CACHE)

                .placeholder(touxiang).into(holder.imge);

        holder.btndel.setOnClickListener(new View.OnClickListener() {


            @Override
            public void onClick(View view) {
                if(userid== Distinguishid){ //判断userid相等
                    int userid=beanList.get(position).getUserid();
                    String Roomname=beanList.get(position).getRoomname();
                    chengyuanActivity.deletCheng(userid,Roomname,position);
                }else {
                    Toast.makeText(view.getContext(),"无权限操作",Toast.LENGTH_LONG).show();
                }

                holder.swi.smoothClose();
            }
        });

    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return beanList.size();
    }



    public class ViewHoler extends RecyclerView.ViewHolder {
        private final ImageView imge;
        private final TextView tv_nick;
        private final Button btndel;
        View mview;
        private final SwipeMenuLayout swi;
        public ViewHoler(View itemView) {
            super(itemView);
            mview = itemView;
            swi = (SwipeMenuLayout) itemView.findViewById(R.id.swi);
            btndel = (Button)itemView. findViewById(R.id.btndel);
            imge = (ImageView)itemView. findViewById(R.id.imge);
            tv_nick = (TextView)itemView.findViewById(R.id.tv_nick);
        }
    }
}
